package it.ma.musicplayernew.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import java.util.Timer;
import java.util.TimerTask;

import it.ma.musicplayernew.helper.HeadSetHelper;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/11 11:51
 * Description: TODO
 */
public class HeadSetReceiver extends BroadcastReceiver {

    Timer timer = null;
    HeadSetHelper.OnHeadSetListener headSetListener = null;
    private static boolean isTimerStart = false;
    private static MyTimer myTimer = null;
    private int mKeyCode;
    //重写构造方法，将接口绑定。因为此类的初始化的特殊性。
    public HeadSetReceiver(){
        timer = new Timer(true);
        this.headSetListener = HeadSetHelper.getInstance().getOnHeadSetListener();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String intentAction = intent.getAction() ;
        if(Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
            //获得KeyEvent对象
            KeyEvent keyEvent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if(headSetListener != null){
                try {
                    if(keyEvent.getAction() == KeyEvent.ACTION_UP){
                        int keyCode = keyEvent.getKeyCode();
                        Log.e("keyCode", "keyCode:" + keyCode );
                        if(isTimerStart && mKeyCode == keyCode){
                            myTimer.cancel();
                            isTimerStart = false;
                            headSetListener.onDoubleClick(mKeyCode);
                        }else{
                            myTimer = new MyTimer();
                            timer.schedule(myTimer,10);
                            isTimerStart = true;
                            mKeyCode = keyCode;
                        }
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
        //终止广播(不让别的程序收到此广播，免受干扰)
        abortBroadcast();
    }
    /*
     * 定时器，用于延迟1秒，内若无操作则为单击
     */
    class MyTimer extends TimerTask {

        @Override
        public void run() {
            try {
                myHandle.sendEmptyMessage(0);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    };
    /*
     * 此handle的目的主要是为了将接口在主线程中触发
     * ，为了安全起见把接口放到主线程触发
     */
    Handler myHandle = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            headSetListener.onClick(mKeyCode);
            isTimerStart = false;
        }
    };
}
